package org.soft.carbon_neutral.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.soft.carbon_neutral.entity.Equipment;
import org.soft.carbon_neutral.entity.EquipmentValue;
import org.soft.carbon_neutral.mapper.EquipmentMapper;
import org.soft.carbon_neutral.mapper.EquipmentValueMapper;
import org.soft.carbon_neutral.service.EquipmentService;
import org.soft.carbon_neutral.util.R;
import org.soft.carbon_neutral.vo.EquipmentVO;
import org.soft.carbon_neutral.vo.EquipmentValueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 设备业务层实现层
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private EquipmentValueMapper equipmentValueMapper;

    /**
     * 查找所有的设备
     *
     * @return
     */
    public R selectAll() {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        List<Equipment> equipment = equipmentMapper.selectList(wrapper);
        if (equipment != null) {
            return R.success().data("list", equipment);
        } else {
            return R.error();
        }
    }

    public R getAllEquipmentValue() {
        List<EquipmentValueVO> equipmentValues = equipmentValueMapper.getEquipmentValueList();
        return R.success().data("values",equipmentValues);
    }

    /**
     * 根据设备名称查找
     *
     * @param modelName
     * @return
     */
    public R selectOneByName(String modelName) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq("model_name", modelName);
        Equipment equipment = equipmentMapper.selectOne(wrapper);
        if (equipment != null) {
            return R.success().data("one", equipment);
        } else {
            return R.error();
        }
    }

    /**
     * 根据设备编号查找
     *
     * @param id
     * @return
     */
    public R selectOneById(String id) {
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("id", id);
        Equipment equipment = equipmentMapper.selectOne(equipmentQueryWrapper);
        QueryWrapper<EquipmentValue> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        List<EquipmentValue> equipmentValues = equipmentValueMapper.selectList(wrapper);
        System.out.println(equipmentValues.toString());
        return R.success().data("equip", equipment).data("list", equipmentValues);
    }


    /**
     * 更新设备监测范围或者阈值
     *
     * @param equipment
     * @return
     */
    public R updateOneById(Equipment equipment) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq("id", equipment.getId());
        int update = equipmentMapper.update(equipment, wrapper);
        if (update > 0) {
            return R.success().message("设备更新成功");
        } else {
            return R.error().message("设备更新异常");
        }
    }

    /**
     * 根据id移除设备
     *
     * @param id
     * @return
     */
    public R removeOneById(String id) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        int delete = equipmentMapper.delete(wrapper);
        int delete1 = equipmentValueMapper.delete(new QueryWrapper<EquipmentValue>().eq("id", id));
        if (delete > 0 && delete1 > 0) {
            return R.success().message("设备移除成功");
        } else {
            return R.error().message("设备移除异常");
        }
    }

    /**
     * 新增设备
     *
     * @param equipment
     * @return
     */
    public R addEquipment(Equipment equipment, String type) {
        // 设备信息写入数据库
        Equipment target = new Equipment();
        target.setId(equipment.getId());
        target.setThreshold(equipment.getThreshold());
        target.setLocation(equipment.getLocation());
        target.setCreateTime(equipment.getCreateTime());
        target.setModelName(equipment.getModelName());
        target.setModelType(equipment.getModelType());
        target.setModelUrl(equipment.getModelUrl());

        String modelPosition = equipment.getModelPosition();
        String[] targetPosition = modelPosition.split("\\s+");
        double[] intPosition= new double[targetPosition.length];
        for (int i = 0; i < targetPosition.length; i++) {
            intPosition[i] = Double.parseDouble(targetPosition[i]);
        }
        target.setModelPosition(Arrays.toString(intPosition));


        String modelAngle = equipment.getModelAngle();
        String[] targetAngle = modelAngle.split("\\s+");
        double[] intAngle = new double[targetAngle.length];
        for (int i = 0; i < targetAngle.length; i++) {
            intAngle[i] = Double.parseDouble(targetAngle[i]);
        }
        target.setModelAngle(Arrays.toString(intAngle));

        int insert = equipmentMapper.insert(target);
        if (insert > 0) {
            // 设备检测值初始化
            EquipmentValue equipmentValue = new EquipmentValue(target.getId(), new Date(), "0.03%", type);
            int insert1 = equipmentValueMapper.insert(equipmentValue);
            if (insert1 > 0) {
                return R.success().message("设备新增成功");
            } else {
                return R.error().message("设备新增异常");
            }
        } else {
            return R.error().message("设备新增异常");
        }
    }
}
